/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "MainWindow.h"
#include "Application.h"
#include "TransactionDownloader.h"
#include "RichTextWidget.h"
#include "SymbolSearchWidget.h"
#include "DepositWidget.h"
#include "TransactionDownloadWidget.h"
#include "StockFilterWidget.h"


namespace QSThink
{
	const QString MainWindow::HIERACHY_WIDGET_NAME="Heriachy";
	const QString MainWindow::OBJECT_CREATE_WIDGET_NAME="Object";
	const QString MainWindow::ASSET_WIDGET_NAME="Asset";

	MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
		: QMainWindow(parent, flags),_projectDialog(0),_assetsAnalyzeDialog(0)
	{
		createSystemTray();
		createMenus();

		this->setWindowTitle(ApplicationSingleton.APPLICATION_NAME);
		this->createDefaultUI();


	}

	MainWindow::~MainWindow()
	{

	}

	void MainWindow::createMenus()
	{
		QMenuBar* menuBar=this->menuBar();

		//QMenu* dataMenu = new QMenu(QObject::tr("Data"), this);
		//QAction* downloadAction=dataMenu->addAction(QObject::tr("Download"));
		//menuBar->addMenu(dataMenu);

		QMenu* menuAnalyze = new QMenu(QObject::tr("Analyze"), this);
		menuBar->addMenu(menuAnalyze);

		QAction* assetAnalyzeAction=menuAnalyze->addAction(QObject::tr("Assets"));//view assets list

		QMenu* menuCustom = new QMenu(QObject::tr("Customize"), this);
		menuBar->addMenu(menuCustom);
		QMenu* styleMenu= menuCustom->addMenu(QObject::tr("Style"));

		QAction* WindowsXPStyleAction= styleMenu->addAction(QObject::tr("WindowsXP"));
		QAction* MotifStyleAction= styleMenu->addAction(QObject::tr("Motif"));
		QAction* CDEStyleAction= styleMenu->addAction(QObject::tr("CDE"));
		QAction* PlastiqueStyleAction= styleMenu->addAction(QObject::tr("Plastique"));
		QAction* CleanlooksStyleAction= styleMenu->addAction(QObject::tr("Cleanlooks"));


		QMenu* menuHelp = new QMenu(QObject::tr("Help"), this);
		QAction* docAction= menuHelp->addAction(QObject::tr("Doc"));
		QAction* wikiAction= menuHelp->addAction(QObject::tr("Wiki"));
		QMenu* langMenu=menuHelp->addMenu(QObject::tr("Language"));
		QAction* aboutAction= menuHelp->addAction(QObject::tr("About"));
		menuBar->addMenu(menuHelp);

		langMenu->addAction(QObject::tr("Chinese"));
		langMenu->addAction(QObject::tr("English"));

		setMenuBar(menuBar);


		//connect(downloadAction, SIGNAL(triggered()), this, SLOT(dowloadData()));

		connect(WindowsXPStyleAction, SIGNAL(triggered()), this, SLOT(WindowsXPStyle()));
		connect(MotifStyleAction, SIGNAL(triggered()), this, SLOT(MotifStyle()));
		connect(CDEStyleAction, SIGNAL(triggered()), this, SLOT(CDEStyle()));
		connect(PlastiqueStyleAction, SIGNAL(triggered()), this, SLOT(PlastiqueStyle()));
		connect(CleanlooksStyleAction, SIGNAL(triggered()), this, SLOT(CleanlooksStyle()));
		connect(assetAnalyzeAction, SIGNAL(triggered()), this, SLOT(assetAnalyze()));

		
	}


	void MainWindow::createDefaultUI()
	{
		_mainTab=new QTabWidget(this);
		this->setCentralWidget(_mainTab);

		
		createPortfolioWidget();
		createDepositWidget();
		createScreenerWidget();
		createTransactionDownloadWidget();
		createTransactionViewWidget();

		createFilterWidget();
	}

	void MainWindow::createHierachyDock()
	{
		QDockWidget *dockWidget = new QDockWidget(QObject::tr("Hierachy"), this);
		dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
		
		addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

		_dock0=dockWidget;
	}

	void MainWindow::createObjectDock()
	{
		QDockWidget *dockWidget = new QDockWidget(QObject::tr("Object"), this);
		dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
		addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
		
		_dock2=dockWidget;
	}


	void MainWindow::createAssetsDock()
	{
		QDockWidget *dockWidget = new QDockWidget(QObject::tr("Assets"), this);
		dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
		addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

		_dock1=dockWidget;
	}

	void MainWindow::createPropertyDock()
	{
		QDockWidget *dockWidget = new QDockWidget(QObject::tr("Property"), this);
		dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);

		//dockWidget->setWidget();

		addDockWidget(Qt::RightDockWidgetArea, dockWidget);

		_dock3=dockWidget;
	}

	

	void MainWindow::saveProject()
	{
		
	}


	
	void MainWindow::selectCreateDialog()
	{
		if(!_projectDialog){
			_projectDialog=new ProjectDialog(this);
		}
		_projectDialog->selectCreateTab();
	}

	void MainWindow::selectOpenDialog()
	{
		if(!_projectDialog){
			_projectDialog=new ProjectDialog(this);
		}
		
		_projectDialog->selectOpenTab();
		_projectDialog->show();
	}


	void MainWindow::createPortfolioWidget()
	{
		_portfolioWidget=new PortfolioWidget(this);
		_mainTab->addTab(_portfolioWidget,QObject::tr("Portfolio"));

	}

	void MainWindow::createTransactionViewWidget()
	{
		_transactionViewWidget=new TransactionViewWidget(this);
		_mainTab->addTab(_transactionViewWidget,QObject::tr("Transactions"));

	}

	void MainWindow::dowloadData()
	{
		QDate dateStart(2011,3,21);
		QDate dateEnd=QDate::currentDate();

		//TransactionDownloader::getSingleton().download(600000,dateStart,dateEnd);

	}

	void MainWindow::createScreenerWidget()
	{
		_stockScreenerWidget=new StockScreenerWidget(this);
		_mainTab->addTab(_stockScreenerWidget,QObject::tr("Screener"));

	}

	void MainWindow::createTextEditWidget()
	{
		RichTextWidget* w=new RichTextWidget;
		_mainTab->addTab(w,QObject::tr("Text"));
	}

	void MainWindow::createSearchWidget()
	{
		SymbolSearchWidget* w=new SymbolSearchWidget;
		_mainTab->addTab(w,QObject::tr("Symbol"));
	}

	void MainWindow::createDepositWidget()
	{
		DepositWidget* w=new DepositWidget;
		_mainTab->addTab(w,QObject::tr("Deposit"));
		
	}

	void MainWindow::closeEvent( QCloseEvent *event )
	{
		if (_trayIcon->isVisible()) {
// 			QMessageBox::information(this, tr("Systray"),
// 				tr("The program will keep running in the "
// 				"system tray. To terminate the program, "
// 				"choose <b>Quit</b> in the context menu "
// 				"of the system tray entry."));
			hide();
			event->ignore();
		}

	}

	void MainWindow::iconActivated( QSystemTrayIcon::ActivationReason reason )
	{
		
		
		switch(reason){
			case QSystemTrayIcon::DoubleClick:
				if(this->isHidden()){
					this->showNormal();
					this->activateWindow();
				}else{
					this->hide();
				}
				break;
			default:
				break;
		}
		

	}

	void MainWindow::messageClicked()
	{

	}

	void MainWindow::showTrayMessage(const QString &title, const QString &msg,QSystemTrayIcon::MessageIcon icon, int msecs)
	{
		_trayIcon->showMessage(title,msg,icon,msecs);
	}

	void MainWindow::restoreWindow()
	{
		showNormal();
		activateWindow();
	}

	void MainWindow::WindowsXPStyle()
	{
		Application::getSingleton().setStyle(Style_WindowsXP);
	}

	void MainWindow::MotifStyle()
	{
		Application::getSingleton().setStyle(Style_Motif);

	}

	void MainWindow::CDEStyle()
	{
		Application::getSingleton().setStyle(Style_CDE);

	}

	void MainWindow::PlastiqueStyle()
	{
		Application::getSingleton().setStyle(Style_Plastique);

	}

	void MainWindow::CleanlooksStyle()
	{
		Application::getSingleton().setStyle(Style_Cleanlooks);

	}

	void MainWindow::assetAnalyze()
	{
		if(!_assetsAnalyzeDialog){
			_assetsAnalyzeDialog=new AssetsAnalyzeDialog(this);
		}

		_assetsAnalyzeDialog->show();

	}

	void MainWindow::createTransactionDownloadWidget()
	{
		TransactionDownloadWidget* w=new TransactionDownloadWidget(this);
		_mainTab->addTab(w,QObject::tr("Transaction Download"));
	}

	void MainWindow::sendQuit()
	{
		qApp->quit();
	}

	void MainWindow::createFilterWidget()
	{
		StockFilterWidget* w=new StockFilterWidget(this);
		_mainTab->addTab(w,QObject::tr("Stock Filter"));

	}

}
